#include "main.hpp"

int main(int argc,char** argv){
    using namespace PLATFORM;

    platform_interface* platform_init = nullptr;
    std::string plat;
    log* query_info = nullptr;
    app_layer app;
    try{
        if(argc != 2) 
            throw CONFIG_FILE_NONE;
        
        if(access(argv[1],F_OK) < 0)
            throw ARGC_ARGV_ERROR;

        json config_platform(argv[1]); 
        config_platform.find("platform",plat);
        init_console(config_platform); 
        init_main_thread();

        if(!plat.find("xilinx")){
            platform_init = &xilinx_platform;                      
           register_device(&xilinx_platform,config_platform);     
            query_info = &xilinx_platform;
        }else if(!plat.find("altera")){                             
            platform_init = &altera_platform;                       
           register_device(&altera_platform,config_platform);      
            query_info = &altera_platform;
        }else{
            throw DONT_MATCH;
        }

       if( platform_init == nullptr || query_info == nullptr) 
           throw DONT_MATCH; 

        platform_init->init_class();    
        query_info->info();            

       app(config_platform,platform_init,&com);
       app.app_init();
       app.app_cache_info();

    }catch(MAIN_THREAD_ERROR tmp){
        switch(tmp){
            case CONFIG_FILE_NONE:
                std::cout << "platform thread need config file" << std::endl; return -1;
            case ARGC_ARGV_ERROR:
                std::cout << "config file not exit" << std::endl; return -2;
            case DONT_MATCH:
                std::cout << "can't find match platform,please check your config file format" << std::endl; return -3;
            case CONFIG_ERROR:
                std::cout << "platform config error，please look /var/log/platform.log" << std::endl ; return -4; 
            case INIT_ERROR:
                std::cout << "platform init error,please look /var/log/platform.log" << std::endl; return -5; 
        }
    }

    while(1){
        sleep(3);
    }
    return 0;
}